﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_MyUserControls_TH_ChiTietDonDatHangAdmin : System.Web.UI.UserControl
{
    // edit mode by default is false
    private bool editMode = false;
    private DataSet objDataSet;
    // set up the form
    protected void Page_PreRender(object sender, EventArgs e)
    {
        // check if we must display order details       
        if (Session["AdminOrderID"] != null)
        {
            // fill constituent controls with data
            PopulateControls();
            // set edit mode
            SetEditMode(editMode);
        }
        else
            // Hide
            this.Visible = false;
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // populate the form with data
    private void PopulateControls()
    {
        // obtain order ID from the session
        string orderId = Session["AdminOrderID"].ToString();
        // obtain order info
        DonDatHangBUS OrdersAccess =
            new DonDatHangBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = OrdersAccess.GetInfoDonDatHang(int.Parse(orderId));
        DataRow row = objDataSet.Tables[0].Rows[0];
        DonDatHangDTO orderInfo = new DonDatHangDTO();
        orderInfo.Id = int.Parse(row["id"].ToString());
        orderInfo.ThanhTien = long.Parse(row["ThanhTien"].ToString());
        orderInfo.NgayDatHang = DateTime.Parse(row["NgayDatHang"].ToString());

        if (!row["NgayGiaoHang"].ToString().Equals(""))
        {
            orderInfo.NgayGiaoHang = DateTime.Parse(row["NgayGiaoHang"].ToString());
            dateShippedTextBox.Text = orderInfo.NgayGiaoHang.ToString();
        }
        else
        {
            dateShippedTextBox.Text = "";
        }
        orderInfo.XacNhan = bool.Parse(row["XacNhan"].ToString());
        orderInfo.DaGiao = bool.Parse(row["DaGiao"].ToString());
        orderInfo.GhiChu = row["GhiChu"].ToString();
        orderInfo.TenKhachHang = row["TenKhachHang"].ToString();
        orderInfo.DiaChiNguoiNhan = row["DiaChiNguoiNhan"].ToString();
        // populate labels and text boxes with order info
        orderIdLabel.Text = "Hiển thị đơn đặt hàng #" + orderId;
        totalAmountLabel.Text = String.Format("{0:#,##0;Nothing}", orderInfo.ThanhTien);
        dateCreatedTextBox.Text = orderInfo.NgayDatHang.ToString();
        //dateShippedTextBox.Text = orderInfo.NgayGiaoHang.ToString();
        verifiedCheck.Checked = orderInfo.XacNhan;
        completedCheck.Checked = orderInfo.DaGiao;
        commentsTextBox.Text = orderInfo.GhiChu;
        customerNameTextBox.Text = orderInfo.TenKhachHang;
        shippingAddressTextBox.Text = orderInfo.DiaChiNguoiNhan;
        // by default the Edit button is enabled, and the
        // Update and Cancel buttons are disabled
        editButton.Enabled = true;
        updateButton.Enabled = false;
        cancelButton.Enabled = false;
        // Decide which one of the other three buttons
        // should be enabled and which should be disabled
        if (completedCheck.Checked)
        {
            // if the order was canceled or completed ...
            editButton.Enabled = false;
            markVerifiedButton.Enabled = false;
            markCompletedButton.Enabled = false;
        }
        else if (verifiedCheck.Checked)
        {
            editButton.Enabled = true;
            // if the order was not canceled but is verified ...
            markVerifiedButton.Enabled = false;
            markCompletedButton.Enabled = true;
        }
        else
        {
            // if the order was not canceled and is not verified ...
            editButton.Enabled = true;
            markVerifiedButton.Enabled = true;
            markCompletedButton.Enabled = false;
        }
        // fill the data grid with order details
        objDataSet = OrdersAccess.GetDetailsDonDatHang(int.Parse(orderId));
        grid.DataSource = objDataSet.Tables[0];
        grid.DataBind();
    }

    // enable or disable edit mode
    private void SetEditMode(bool enable)
    {
        dateCreatedTextBox.Enabled = enable;
        dateShippedTextBox.Enabled = enable;
        verifiedCheck.Enabled = enable;
        completedCheck.Enabled = enable;
        commentsTextBox.Enabled = enable;
        customerNameTextBox.Enabled = enable;
        shippingAddressTextBox.Enabled = enable;
        updateButton.Enabled = enable;
        cancelButton.Enabled = enable;
    }
    protected void editButton_Click(object sender, EventArgs e)
    {
        editMode = true;
    }
    protected void updateButton_Click(object sender, EventArgs e)
    {
        // Store the new order details in an OrderInfo object
        DonDatHangDTO orderInfo = new DonDatHangDTO();
        string orderId = Session["AdminOrderID"].ToString();
        orderInfo.Id = Int32.Parse(orderId);
        orderInfo.NgayDatHang = DateTime.Parse(dateCreatedTextBox.Text);

        int type = 0;
        if (!dateShippedTextBox.Text.Equals(""))
        {
            type = 1;
            orderInfo.NgayGiaoHang = DateTime.Parse(dateShippedTextBox.Text);
        }

        orderInfo.XacNhan = verifiedCheck.Checked;
        orderInfo.DaGiao = completedCheck.Checked;
        orderInfo.GhiChu = commentsTextBox.Text;
        orderInfo.TenKhachHang = customerNameTextBox.Text;
        orderInfo.DiaChiNguoiNhan = shippingAddressTextBox.Text;

        // try to update the order 
        try
        {
            // Update the order
            DonDatHangBUS OrdersAccess = new DonDatHangBUS(TGDDConfiguration.DbConnectionString);
            OrdersAccess.UpdateDonDatHang(orderInfo, type);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
            // In case of an error, we simply ignore it
        }
        // Exit edit mode 
        SetEditMode(false);
        // Update the form
        PopulateControls();
    }
    protected void cancelButton_Click(object sender, EventArgs e)
    {
        // don't need to do anything, editMode will be set to false by default
    }
    protected void markVerifiedButton_Click(object sender, EventArgs e)
    {
        // obtain the order ID from the session
        string orderId = Session["AdminOrderID"].ToString();
        // mark order as verified
        DonDatHangBUS OrdersAccess = new DonDatHangBUS(TGDDConfiguration.DbConnectionString);
        OrdersAccess.MarkVerifiedDonDatHang(int.Parse(orderId));
        // update the form
        PopulateControls();
    }
    protected void markCompletedButton_Click(object sender, EventArgs e)
    {
        // obtain the order ID from the session
        string orderId = Session["AdminOrderID"].ToString();
        // mark the order as completed
        DonDatHangBUS OrdersAccess = new DonDatHangBUS(TGDDConfiguration.DbConnectionString);
        OrdersAccess.MarkCompletedDonDatHang(int.Parse(orderId));
        // update the form
        PopulateControls();
    }
    protected void markCanceledButton_Click(object sender, EventArgs e)
    {
        // obtain the order ID from the session
        string orderId = Session["AdminOrderID"].ToString();
        // mark the order as canceled
        DonDatHangBUS OrdersAccess = new DonDatHangBUS(TGDDConfiguration.DbConnectionString);
        OrdersAccess.MarkCanceledDonDatHang(int.Parse(orderId));
        // update the form
        PopulateControls();
    }
    protected void btnInHoaDon_Click(object sender, EventArgs e)
    {
        string orderId = Session["AdminOrderID"].ToString();
        Response.Redirect("InHoaDon.aspx?id=" + orderId);
    }
}
